# 生成数据,数据样式：
# x：[[ 0.05175328 -0.91952579  1.23943184]      y类别:[2 2]
#     [-0.06851406 -1.09510487  1.42925019]]
# (1)LDA为有监督降维，PCA为无监督降维。
# (2)PCA可以降低到任意维度，LDA只能降维最大至类别数减1。
# (3)LDA可以进行分类
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from numpy import array
from sklearn.datasets import make_classification

x, y = make_classification(n_samples=1000, n_features=3, n_redundant=0, n_classes=3, n_informative=2,
                           n_clusters_per_class=1, class_sep=0.5, random_state=10)

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x[:, 0], x[:, 1], x[:, 2], c=y)

# PCA
# 输入x:[[ 0.05175328 -0.91952579  1.23943184]
#       [-0.06851406 -1.09510487  1.42925019]]
# 输出x1:[[-0.77856636  0.97415331]
#        [-1.01408135  1.13368518]]
from sklearn.decomposition import PCA

model1 = PCA(n_components=2)  # 将维至任意维度（这里是2维）
model1.fit(x)
x1 = model1.transform(x)
plt.scatter(x1[:, 0], x1[:, 1], c=y)
plt.show()

# 预测
xa = array([[0.05175328, -0.91952579, 1.23943184]])
xa1 = model1.transform(xa)
print(xa1)
